package com.sl.controller;

import com.alibaba.fastjson.JSONObject;
import com.sl.entity.Result;
import com.sl.entity.StatusCode;
import com.sl.vo.RouteMsg;
import com.sl.pojo.Route;
import com.sl.service.RouteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/route")
public class RouteController {

    @Autowired
    private RouteService routeService;
    //@RequiresPermissions("route:all")
    @PostMapping("/all")
    public Result findAll() {
        List<Route> all = routeService.findAll();
        //封装成MSG
       List<RouteMsg>  routeList=new ArrayList<>();

        for (Route routeManager : all) {
            RouteMsg   routeMsg = DtoRouteMsg(routeManager);
            Integer id = routeManager.getId();
            List<Route> byPidList = routeService.findByPid(id);
            List<RouteMsg> msgPidList = new ArrayList<>();

            for (Route route : byPidList) {
                RouteMsg dtoRouteMsg = DtoRouteMsg(route);
                msgPidList.add(dtoRouteMsg);
            }
            routeMsg.setChildren(msgPidList);
            routeList.add(routeMsg);
        }
        log.info("total>>>" + routeList.size());
        return new Result(true, StatusCode.OK,"查询所有路由成功",routeList);
    }

    private RouteMsg DtoRouteMsg(Route route) {
        RouteMsg routeMsg = new RouteMsg();
        routeMsg.setPath(route.getPath());
        routeMsg.setName(route.getName());
        routeMsg.setComponent(route.getComponent());
        routeMsg.setRedirect(route.getRedirect());

        Map<String,Object> map=new HashMap<>();
        map.put("title",route.getTitle());
        map.put("icon",route.getIcon());
        Integer closeableDB = route.getCloseable();
        boolean closeable = true;
        //1为 false 。 0是 显示
        if (closeableDB !=null && closeableDB !=1){
            closeable= false;
        }
        map.put("closeable",closeable);

        Integer hiddenDB = route.getHidden();
        boolean hidden = true;
        //如果 是1 就是true  隐藏。。
        if (hiddenDB !=null && hiddenDB !=1){
            hidden= false;
        }
        map.put("hidden",hidden);
        routeMsg.setMeta(map);
        return routeMsg;
    }


}
